<template>
  <el-dialog :title="{add:'新增权限点',edit:'编辑权限点'}[mode]" :visible.sync="show" @close="closeEvent">
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item prop="name" label="权限名称">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item prop="code" label="权限标识">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item prop="description" label="权限描述">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item prop="enVisible" label="开启">
        <el-switch v-model="form.enVisible" inactive-value="0" active-value="1" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div style="text-align:center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button>取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermissionPost, sysPermissionPut } from '@/api/permission'
export default {
  data() {
    return {
      mode: 'add',
      show: false,
      form: {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        enVisible: '', //	string	非必须
        type: '', //	number	非必须   1:页面  2：按钮
        pid: '' //	非必须
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须
        code: [{ required: true, message: '必填', trigger: 'change' }] //	string	非必须
      }
    }
  },
  methods: {
    async submit() {
      await this.$refs.form.validate()
      if (this.mode === 'add') {
        // 新增
        await sysPermissionPost(this.form)
        this.$message.success('新增成功')
      } else if (this.mode === 'edit') {
        // 编辑
        await sysPermissionPut(this.form)
        this.$message.success('编辑成功')
      }
      this.show = false
      this.$emit('getData')
    },
    // 清空表单数据与验证
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        enVisible: '', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	非必须
      }
    }
  }
}
</script>
<style>
</style>
